package com.andy.beef.main.activity

import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import com.andy.annotation.BindValue
import com.andy.beef.R
import com.andy.beef.databinding.ActivityMainBinding
import com.andy.beef.main.fragment.HomeFragment
import com.andy.beef.main.fragment.MineFragment
import com.andy.beef.main.fragment.WorkbenchFragment
import com.andy.beef.main.viewmodel.MainViewModel
import com.andy.module.adapter.SimpleViewPagerAdapter
import com.andy.module.base.BaseVbActivity
import com.andy.module.util.PermissionHelper
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import kotlin.system.exitProcess

/**
 * @creator:LuoFan
 * @date:2022/5/26
 * @description:
 */
class MainActivity : BaseVbActivity<MainViewModel, ActivityMainBinding>() {

    private lateinit var viewPagerAdapter: SimpleViewPagerAdapter

    private var clickBackTime = 0L

    @BindValue("id")
    var farmId: String? = null

    @RequiresApi(Build.VERSION_CODES.M)
    override fun createObserver() {
        super.createObserver()
        val listener = object : PermissionUtils.SimpleCallback {
            override fun onGranted() {
                //TODO
            }

            override fun onDenied() {
                //TODO
            }
        }
        PermissionHelper.request(this, listener, PermissionConstants.STORAGE)
        initBindValue()
        LogUtils.e("farmId=" + farmId)
    }

    private fun initBindValue() {
        val fields = this.javaClass.declaredFields
        for (field in fields) {
            val annotation = field.getAnnotation(BindValue::class.java)
            annotation?.let {
                farmId = intent?.getStringExtra(it.value)
                LogUtils.e("farmId" + farmId)
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        val fragmentList = mutableListOf<String>()
        fragmentList.add(HomeFragment::class.java.name)
        fragmentList.add(WorkbenchFragment::class.java.name)
        fragmentList.add(MineFragment::class.java.name)
        viewPagerAdapter = SimpleViewPagerAdapter(this, fragmentList)
        mViewBind.mainViewPager.adapter = viewPagerAdapter
        mViewBind.mainViewPager.isUserInputEnabled = false
        mViewBind.mainViewPager.offscreenPageLimit = 3
        mViewBind.mainNavigation.setOnItemSelectedListener {
            when (it.itemId) {
                R.id.home -> {
                    mViewBind.mainViewPager.setCurrentItem(0, false)
                }
                R.id.workbench -> {
                    mViewBind.mainViewPager.setCurrentItem(1, false)
                }
                R.id.mine -> {
                    mViewBind.mainViewPager.setCurrentItem(2, false)
                }
            }
            true
        }
    }

    override fun onBackPressed() {
        exit()
    }

    private fun exit() {
        if (System.currentTimeMillis() - clickBackTime > 2000 || clickBackTime == 0L) {
            clickBackTime = System.currentTimeMillis()
            ToastUtils.showShort(R.string.sure_exit)
        } else {
            exitProcess(0)
        }
    }
}